package com.wu.shop.module.main.ui.activity

import android.Manifest
import android.os.Bundle
import com.wkq.base.frame.activity.MvpBindingActivity
import com.wkq.base.utils.AlertUtil
import com.wkq.kd.MainPresenter
import com.wkq.kd.MainView
import com.wu.common.base.ui.activity.FinishAllActivity
import com.wu.common.base.ui.activity.MvpBaseActivity
import com.wu.common.util.screen.ScreenShotListener
import com.wu.shop.R
import com.wu.shop.databinding.ActivityMainBinding
import com.wu.shop.wxapi.WechatLoginObservable.getInstance
import pub.devrel.easypermissions.EasyPermissions

class MainActivity : MvpBaseActivity<MainView, MainPresenter, ActivityMainBinding>() {

    private var mainBackPressedTime: Long = 0
    private val RC_READ_WRITE_EXTERNAL_STORAGE = 10000
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        screenShotListener = ScreenShotListener(this)
        if (mvpView != null) mvpView.initView()
        requestPermissionsA()

    }
    private fun requestPermissionsA() {
        val perms =
                arrayOf(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                )

        //判断有没有权限
        if (EasyPermissions.hasPermissions(this, *perms)) { // 如果有权限了, 就做你该做的事情
            /*val PATH: String =
                Environment.getExternalStorageDirectory().path + File.separator + Environment.DIRECTORY_DCIM + File.separator + "Screenshots"
            ScreenShotFileObserver(PATH).startWatching()*/
        } else { // 如果没有权限, 就去申请权限
            // this: 上下文
            // Dialog显示的正文
            // RC_CAMERA_AND_RECORD_AUDIO 请求码, 用于回调的时候判断是哪次申请
            // perms 就是你要申请的权限
            EasyPermissions.requestPermissions(
                    this,
                    "写上你需要用权限的理由, 是给用户看的",
                    RC_READ_WRITE_EXTERNAL_STORAGE,
                    *perms
            )
        }
    }


    override fun getLayoutId(): Int {

        return R.layout.activity_main
    }

    override fun onResume() {
        super.onResume()
        startScreenShotListen()
    }

    override fun onPause() {
        super.onPause()
        startScreenShotListen()
    }

    private lateinit var screenShotListener: ScreenShotListener
    var isHasScreenShotListener = false
    private fun startScreenShotListen() {
        if (!isHasScreenShotListener) {
            screenShotListener.setListener(object : ScreenShotListener.OnScreenShotListener {
                override fun onScreenShot(picPath: String) {
                    runOnUiThread {
                        mvpView.showMessage(picPath)
                    }
                }
            })
            screenShotListener.startListener()
            isHasScreenShotListener = true
        }
    }

    private fun stopScreenShotListen() {
        if (isHasScreenShotListener) {
            screenShotListener.stopListener()
            isHasScreenShotListener = false
        }
    }


    /**
     * 返回
     */
    override fun onBackPressed() {
        if (mainBackPressedTime + 2000 > System.currentTimeMillis()) {
            super.onBackPressed()
            // 结束进程（热修复准备完成）
            FinishAllActivity.finish(this)
        } else {
            AlertUtil.showDeftToast(this, "再次点击 退出应用")
            mainBackPressedTime = System.currentTimeMillis()
        }

    }
}
